package com.wtwd.campus.controller;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.encryptnetty.EncryptChannelHandler;
import com.wtwd.campus.utils.UnicodeUtil;
import io.netty.channel.Channel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author ldaoliang
 * @Date create in 9:37 2021/9/17
 * @Description 喜马拉雅新协议测试Controller
 */
@RestController
public class TestXmlyNewProtocolController {
    private static final Logger logger = LoggerFactory.getLogger(TestXmlyNewProtocolController.class);

    @PostMapping("/setAlarmClock")
    public RespEntity setAlarmClock(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        Integer sequence = jsonObject.getInteger("sequence");
        String time = jsonObject.getString("time");
        String repeatTime = jsonObject.getString("repeatTime");
        Integer onOff = jsonObject.getInteger("onOff");
        String name = jsonObject.getString("name");
        String commandNumber = jsonObject.getString("commandNumber");
        name = StringUtils.isBlank(name) ? "" : UnicodeUtil.gbEncoding(name);
        String content = "*WT," + imei + ",ALARMCLOCK," + commandNumber + "," + sequence + "," + name
                + "," + time + "," + repeatTime + "," + onOff + "#";
        Channel channel = EncryptChannelHandler.getChannel(imei);
        if (channel != null) {
            channel.writeAndFlush(content);
            System.out.println("消息[" + content + "]发送成功");
        } else {
            System.out.println("设备离线");
        }
        return new RespEntity(RespCode.SUCCESS);
    }

    @PostMapping("/delAlarmClock")
    public RespEntity delAlarmClock(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        Integer sequence = jsonObject.getInteger("sequence");
        String commandNumber = jsonObject.getString("commandNumber");
        String content = "*WT," + imei + ",DELCLOCK," + commandNumber + "," + sequence + "#";
        Channel channel = EncryptChannelHandler.getChannel(imei);
        if (channel != null) {
            channel.writeAndFlush(content);
            System.out.println("消息[" + content + "]发送成功");
        } else {
            System.out.println("设备离线");
        }
        return new RespEntity(RespCode.SUCCESS);
    }

    @PostMapping("/sendBindingStatus")
    public RespEntity sendBindingStatus(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        String commandNumber = jsonObject.getString("commandNumber");
        Integer status = jsonObject.getInteger("status");
        String schoolName = jsonObject.getString("schoolName");
        String classesName = jsonObject.getString("classesName");
        String studentName = jsonObject.getString("studentName");
        schoolName = StringUtils.isBlank(schoolName) ? "" : UnicodeUtil.gbEncoding(schoolName);
        classesName = StringUtils.isBlank(classesName) ? "" : UnicodeUtil.gbEncoding(classesName);
        studentName = StringUtils.isBlank(studentName) ? "" : UnicodeUtil.gbEncoding(studentName);
        String content = "*WT," + imei + ",BINDINGSTATUS," + commandNumber + "," + status + ","
                + schoolName + "," + classesName + "," + studentName + "#";
        logger.info("发给喜马拉雅设备绑定关系，指令为：" + content);
        Channel channel = EncryptChannelHandler.getChannel(imei);
        if (channel != null) {
            channel.writeAndFlush(content);
            System.out.println("消息[" + content + "]发送成功");
        } else {
            System.out.println("设备离线");
        }
        return new RespEntity(RespCode.SUCCESS);
    }

    @PostMapping("/sendTextInform")
    public RespEntity sendTextInform(@RequestBody JSONObject jsonObject) {
        String imei = jsonObject.getString("imei");
        String commandNumber = jsonObject.getString("commandNumber");
        String title = jsonObject.getString("title");
        String informContent = jsonObject.getString("content");
        String content = "*WT," + imei + ",TEXTINFORM," + commandNumber + "," + UnicodeUtil.gbEncoding(title) + ","
                + UnicodeUtil.gbEncoding(informContent) + "#";
        Channel channel = EncryptChannelHandler.getChannel(imei);
        if (channel != null) {
            channel.writeAndFlush(content);
            System.out.println("消息[" + content + "]发送成功");
        } else {
            System.out.println("设备离线");
        }
        return new RespEntity(RespCode.SUCCESS);
    }

}
